package com.windoent.yugioh.ui.activity.meinfo;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.GeneralBean;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/8 18:01
 * desc   :店家认证
 */
public class StoreAuthActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private EditText mEtDianpuId;
    private EditText mEtDianpuPhone;
    private Button mBtnGetCode;


    @Override
    public int getLayout() {
        return R.layout.activity_store_auth;
    }

    @Override
    public void initView() {
        super.initView();
        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());

        mEtDianpuId = (EditText) findViewById(R.id.et_dianpu_id);
        mEtDianpuPhone = (EditText) findViewById(R.id.et_dianpu_phone);
        mBtnGetCode = (Button) findViewById(R.id.btn_get_code);

    }

    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mBtnGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取验证码
                String dianpuId = mEtDianpuId.getText().toString().trim();
                String dianpuPhone = mEtDianpuPhone.getText().toString().trim();
                if (BaseMethod.isNull(dianpuId)) {
                    ToastUtils.showText(getString(R.string.string_input_dianpu_id));
                } else if (BaseMethod.isNull(dianpuPhone)) {
                    ToastUtils.showText(getString(R.string.string_input_phone));
                } else {
                    authDianjia(dianpuId, dianpuPhone);
                }
            }
        });
    }

    //认证商家
    private void authDianjia(String dianpuId, String dianpuPhone) {
        showLoadingDialog("");
        PhoneLive.authDianjia(getUserToken(), dianpuId, dianpuPhone, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            getCellPhoneCode(dianpuPhone, dianpuId);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }

            }
        });
    }

    //发送验证码
    private void getCellPhoneCode(String phone, String dianpuId) {
        showLoadingDialog("");
        PhoneLive.getCellPhoneCode(phone, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            GeneralBean generalBean = gson.fromJson(response, GeneralBean.class);
                            ToastUtils.showText(generalBean.getMsg());
                            Intent intent = new Intent(StoreAuthActivity.this, StoreAuthPhoneCodeActivity.class);
                            intent.putExtra("dianpu_id", dianpuId);
                            intent.putExtra("dianpu_phone", phone);
                            startActivity(intent);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "店家认证界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "店家认证界面");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }
}
